/**
  * clasa skupiajaca wszytkie wlasciwosci obrazka szukanego
 * 1. konstruktor - String n, BufferedImage imgtemp
 * @param imgtemp szukany obrazek
 *
 */
package A002_Obrazek;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.TreeMap;

import javax.imageio.ImageIO;

import Linki._A_Path;

//import DaneObrazka.WczytajObrazki;

/**
 * @author Tomek2
 *
 */
public class _A_DaneObrazka2 implements _obrazek{
	
	private String NazwaObrazka;
	
	public _A_DaneObrazka2(String NazwaObrazka) {
		this.NazwaObrazka = NazwaObrazka;
	}

	public BufferedImage KopiujObraz(BufferedImage _ob) {
	
		BufferedImage _tmp = new BufferedImage (_ob.getWidth(), _ob.getHeight(), _ob.getType());
			
			for (int y = 0 ; y < _ob.getHeight() ; y++){
				for (int x = 0 ; x < _ob.getWidth() ; x++){
					_tmp.setRGB(x, y, _ob.getRGB(x, y)); 
				}
			}
			
			return _tmp;
	}

	/**
	 * Wymazuje biale pola w okolo
	 * @param _filtr2 
	 * @return 
	 */
	public BufferedImage crop (BufferedImage _filtr2, BufferedImage _obrazek){
		int gora = 0;
		int lewa = 0;
		int prawa = 0;
		int dol = 0;
		
		//gorna belka
		skok:{
		for (int yG = 0; yG < _filtr2.getHeight(); yG++){
			for (int xL = 0; xL < _filtr2.getWidth(); xL++){
					if (_filtr2.getRGB(xL, yG) != -1){
						gora = yG;
						break skok;
					}
			}
		}
		}
		//Lewa Belka
		skok:{
		for (int xL = 0; xL < _filtr2.getWidth(); xL++){
			for (int yG = 0; yG < _filtr2.getHeight(); yG++){
					if (_filtr2.getRGB(xL, yG) != -1){
						lewa = xL;
						break skok;
					}
			}
		}
		}
		//dolna belka
		skok:{
		for (int yG = _filtr2.getHeight()-1; yG >= 0 ; yG--){
			for (int xL = _filtr2.getWidth()-1; xL >= 0 ; xL--){
					if (_filtr2.getRGB(xL, yG) != -1){
						dol = yG+1;
						break skok;
					}
			}
		}
		}
		//Prawa Belka
		skok:{
		for (int xL = _filtr2.getWidth()-1; xL >= 0; xL--){
			for (int yG = _filtr2.getHeight()-1; yG >= 0 ; yG--){
					if (_filtr2.getRGB(xL, yG) != -1){
						prawa = xL+1;
						break skok;
					}
			}
		}
		}
		//System.out.println("Lewa " + lewa + " Gora "+ gora +" Prawa " + prawa + " Dol " + dol);
		BufferedImage _tmpc = null;
		try{
			_tmpc = new BufferedImage(Math.abs(prawa-lewa), Math.abs(dol-gora), _filtr2.getType());

		} catch (IllegalArgumentException a){
			System.err.println("ZLY ARGUMENT - nie kropuję obrazka" + a.getMessage() + " " + this.NazwaObrazka);
			return _obrazek;
		}

		for (int y = 0, y2 = gora; y < Math.abs(dol-gora); y++,y2++){
			for (int x = 0, x2 = lewa; x < Math.abs(prawa-lewa); x++, x2++){
				
				_tmpc.setRGB(x, y, _filtr2.getRGB(x2, y2));
			}
		}
		
		return _tmpc;
	}
	
	
	
	/**
	 * Utwaia pierwszy i ostatni punkt szukania
	 * koniecznie musi byc po kropowaniu
	 */
	public int[] _ustalKrance (BufferedImage _filtrCrop){
		boolean diag = false;//---------------
		int x1 = 0,  y1, PW = 0, x2 = 0, y2 = 0, OW = 0;
			
		ex1:{
			for (int y = 0 ; y < _filtrCrop.getHeight() ; y++){
				for (int x = 0 ; x < _filtrCrop.getWidth() ; x++){
					if (_filtrCrop.getRGB(x, y) != -1){
						x1 = x;
						y1 = y;
						PW = _filtrCrop.getRGB(x, y);

						if (diag){
							System.out.println(x);
							System.out.println(PW);
						}

						break ex1;
					};
					
				}
			}
		}
		
		ex2:{
			for (int y = _filtrCrop.getHeight()-1 ; y >= 0 ; y--){
				for (int x = _filtrCrop.getWidth()-1 ; x >= 0 ; x--){
					
					if (_filtrCrop.getRGB(x, y) != -1){
						x2 = x;
						y2 = y;
						OW =_filtrCrop.getRGB(x, y);
						if (diag){
							System.out.println(x);
							System.out.println(OW);
						}
						break ex2;
					};
					
				}
			}
		}
		int [] dane = {x1, x2, PW, x2, y2, OW}; 
		return dane;
		
		
		
	}
	
	
	public int _sumaKontrolnaPix(BufferedImage _filtr) { // 5
		int _iPdoZ = 0;
		
		for (int y = 0;y < _filtr.getHeight(); y++){
			for(int x = 0;x < _filtr.getWidth(); x++){
				if (_filtr.getRGB(x, y) != -1)_iPdoZ++;
			}
		}
		
		if (_iPdoZ==0) _iPdoZ = -1;
		return _iPdoZ; 
	}
	
	
	/*// zmiana z 2D do 1D
	private int [] convert2Ddo1D(int obraz2D[][]){

		//System.out.println("BuferedImage - Obrazek SZ-" + obraz2D.length +" W-"+ obraz2D[0].length);

		int[] obraz1D = new int [obraz2D.length * obraz2D[0].length];
		int lp = 0;
		for (int y = 0; y < obraz2D[0].length; y++){
			for ( int x = 0; x < obraz2D.length; x++){
				
				obraz1D[lp] = obraz2D[x][y];
				lp++;
			}
		}		 
		  return obraz1D;
	}*/
	
	
	public void zapiszobraz(String _NazwaObrazu, BufferedImage _obraze, BufferedImage _filtr, BufferedImage _filtrCrop) {
		String path = _A_Path.iGlobalPath + _A_Path.iKatSys + "TestoweDaneObrazka/";
		
		try {
			//ImageIO.write(_obraze, "bmp", new File(path + "001-" + _NazwaObrazu +"_obrazek.bmp"));
			//ImageIO.write(_filtr, "bmp", new File(path + "002-" + _NazwaObrazu +"_filtr.bmp"));
			ImageIO.write(_filtrCrop, "bmp", new File(path + "003-" + _NazwaObrazu +"_filtrCrop.bmp"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public
	TreeMap<Integer, Integer> _ustawKolorFix(TreeMap<Integer,Integer> _kolorFix2, BufferedImage[] _obrazek2) {
		for (BufferedImage ob: _obrazek2){
			for (int y = 0; y < ob.getHeight(); y++){
				for (int x = 0; x < ob.getWidth(); x++){
					int kolor = ob.getRGB(x, y);
					_kolorFix2.put(kolor, kolor);
				}
			}
		}
		return _kolorFix2;
	}
	
	


}
